package com.suse.ecommerce.serviceImp;


import java.util.Set;

import com.suse.ecommerce.service.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Service
public class RedisService {
	@Autowired(required=false)
	private ShardedJedisPool shardedJedisPool;
	private <T> T execute(Function<T, ShardedJedis> fun) {
		ShardedJedis shardedJedis=null;
		try {
			shardedJedis=shardedJedisPool.getResource();
			return fun.callback(shardedJedis);
		} finally{
			if(null!=shardedJedis) {
				shardedJedis.close();
			}
		}
	}
	public String set(final String key,final String value,final Integer seconds) {
		return this.execute(new Function<String, ShardedJedis>() {
			@Override
			public String callback(ShardedJedis e) {
				String set = e.set(key, value);
				e.expire(key, seconds);
				return set;
			}
			
		});
	}
	
	public Long zsetAdd(final String key,final Double score,final String value) {
		return this.execute(new Function<Long, ShardedJedis>() {

			@Override
			public Long callback(ShardedJedis e) {
				return e.zadd(key, score,value);
			}
		
		});
	}
	public String get(final String key) {
		return this.execute(new Function<String, ShardedJedis>() {
			@Override
			public String callback(ShardedJedis e) {
				
				return e.get(key);
			}
		});
	}
	public Long del(final String key) {
		return this.execute(new Function<Long, ShardedJedis>() {

			@Override
			public Long callback(ShardedJedis e) {
				return e.del(key);
			}
			
		});
	}
	public Set<String> zsetRange(final String key,final Long pn){
		return this.execute(new Function<Set<String>, ShardedJedis>() {
			@Override
			public Set<String> callback(ShardedJedis e) {
				return e.zrange(key, pn, pn);
			}
			
		});
	}

	public Long hset(final String key,final  Double  score ,final String menber){
		return this.execute(new Function<Long, ShardedJedis>() {
			@Override
			public Long callback(ShardedJedis shardedJedis) {
				return shardedJedis.zadd(key,score,menber);
			}
		});
	}

	public Long decrement(final String s) {
		return this.execute(new Function<Long, ShardedJedis>() {
			@Override
			public Long callback(ShardedJedis shardedJedis) {
				return shardedJedis.decr(s);
			}
		});
	}

	public Long increment(final  String s){
		return this.execute(new Function<Long, ShardedJedis>() {
			@Override
			public Long callback(ShardedJedis shardedJedis) {
				Long incr = shardedJedis.incr(s);
				return incr;
			}
		});
	}

	public Long zrem(final String table,final String field){
		return this.execute(new Function<Long, ShardedJedis>() {
			@Override
			public Long callback(ShardedJedis shardedJedis) {
				return shardedJedis.zrem(table,field);
			}
		});
	}
	public Double zscore(final String table,final String field){
		return this.execute(new Function<Double, ShardedJedis>() {
			@Override
			public Double callback(ShardedJedis shardedJedis) {
				return shardedJedis.zscore(table,field);
			}
		});
	}
	public Long expire(final String key,final Integer time){
		return this.execute(new Function<Long, ShardedJedis>() {
			@Override
			public Long callback(ShardedJedis shardedJedis) {
				return shardedJedis.expire(key,time);
			}
		});
	}
}
